home *** CD-ROM | disk | FTP | other *** search
- Path: hpcvsnz.cv.hp.com!news
- From: Brian Dixon <briand@cv.hp.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Why use private class members instead of protected?
- Date: 16 Jan 1996 15:59:53 GMT
- Organization: Hewlett Packard Ink-Jet Business Unit
- Message-ID: <4dgi1p$m5l@hpcvsnz.cv.hp.com>
- References: <30F4AB49.6ABB@sierra.net> <30F50874.15FB7483@intellektik.informatik.th-darmstadt.de> <4dgd18$as0@mercury.wright.edu>
- NNTP-Posting-Host: hpcvibsd.cv.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.03 9000/747)
- X-URL: news:4dgd18$as0@mercury.wright.edu
-
- >>> I'm relatively new to c++. I have one quick question: If child
- >>> classes can only access protected members of the parent class,
- >>> why make any members of any class private? Wouldn't it be
- >>> better to make members of the parent class protected so that the
- >>> class is alway "inheritance ready"?
- >>No,
- >>it's quite often a better approach to make these data-members private and
- >>provide the suitable accessor functions. In this way you can change the
- >>representation of the data-members without the problem that modifications
- >>will 'propagate' downwards the class-hierarchy.
- >
- >I agree. However, there are still instances when one would make a data
- >member protected. In particular, very basic (as in linked list) classes
- >with a stable representation should have protected data members. Why? For
- >two reasons.
- >
- > 1. Being basic classes, they (and their derived classes) are likely to
- > be heavily used, especially in inner loops. Even minor
- > optimizations, such as eliminating copying for an accessor function
- > would have benefits throughout your source code.
- >
- > 2. Since they have a stable, efficient representation, and have been
- > studied for years, the representation is unlikely to change and
- > cause problems throughout the class hierarchy.
- >
-
- I disagree. If the data representation is that simple, then 'inline' the
- accessor function or define it within the class definition so it will be
- automatically inlined. That will keep your "tried and true" algorithms
- the same and you don't pay a function call penalty. But as you can tell
- from my answer, I do agree with using accessor functions to prevent down-
- propagation of data representation errors into derived classes.
-
- Brian
-
-